package com.hnchances.wjb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnchances.wjb.Dto.GradesDto;
import com.hnchances.wjb.Service.Impl.*;
import com.hnchances.wjb.common.R;
import com.hnchances.wjb.entity.*;
import com.hnchances.wjb.entity.Class;
import com.hnchances.wjb.mapper.GradesMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;

@Slf4j
@RestController
@RequestMapping("/grades")
public class GradesController {

    @Autowired
    GradesMapper gradesMapper;
    @Autowired
    GradesServiceImpl gradesService;
    @Autowired
    CourseServiceImpl courseService;
    @Autowired
    ClassServiceImpl classService;
    @Autowired
    ExamServiceImpl examService;
    @Autowired
    StudentServiceImpl studentService;


    @RequiresRoles(value =  "teacher")
@RequiresPermissions("roles:grades:add")
    @PostMapping("/add")
    @ApiOperation("老师新增学生的成绩")
    public R<String> addGrades(@RequestBody GradesDto gradesDto){
        Grades grades = new Grades();

        //获取学生的studentId
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(Student::getName,gradesDto.getName());
        int studentId = studentService.getOne(studentLambdaQueryWrapper).getId();
        //获取课程的classId
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classLambdaQueryWrapper.eq(Class::getClassName,gradesDto.getClassName());
        int classId = classService.getOne(classLambdaQueryWrapper).getId();

        //获取courseId
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(Course::getCourseName,gradesDto.getCourseName());
        int courseId = courseService.getOne(courseLambdaQueryWrapper).getId();

        //获取examId
        LambdaQueryWrapper<Exam> examLambdaQueryWrapper = new LambdaQueryWrapper<>();
        examLambdaQueryWrapper.eq(Exam::getExamName,gradesDto.getExamName());
        int examId = examService.getOne(examLambdaQueryWrapper).getId();

        grades.setClassID(classId);
        grades.setCourseID(courseId);
        grades.setExamID(examId);
        grades.setStudentId(studentId);
        grades.setGrades(gradesDto.getGrades());
        gradesService.save(grades);
        return R.success("新增学生成绩成功");
    }

    @RequiresRoles(value =  "teacher")
@RequiresPermissions("roles:grades:del")
    @ApiOperation("老师删除学生的成绩")
    @PostMapping("/del")
    public R<String> delGrades(@RequestBody GradesDto gradesDto){
        Grades grades = new Grades();

        //获取学生的studentId
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(Student::getName,gradesDto.getName());
        int studentId = studentService.getOne(studentLambdaQueryWrapper).getId();
        //获取课程的classId
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classLambdaQueryWrapper.eq(Class::getClassName,gradesDto.getClassName());
        int classId = classService.getOne(classLambdaQueryWrapper).getId();

        //获取courseId
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(Course::getCourseName,gradesDto.getCourseName());
        int courseId = courseService.getOne(courseLambdaQueryWrapper).getId();

        //获取examId
        LambdaQueryWrapper<Exam> examLambdaQueryWrapper = new LambdaQueryWrapper<>();
        examLambdaQueryWrapper.eq(Exam::getExamName,gradesDto.getExamName());
        int examId = examService.getOne(examLambdaQueryWrapper).getId();

        LambdaQueryWrapper<Grades> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Grades::getStudentId,studentId)
                          .eq(Grades::getClassID,classId)
                          .eq(Grades::getCourseID,courseId)
                          .eq(Grades::getExamID,examId);

       Grades grades1 = gradesService.getOne(lambdaQueryWrapper);

       //软删除 将状态改为 1
       grades1.setStatus(1);
       gradesService.updateById(grades1);

        return R.success("学生成绩删除成功");
    }

    @RequiresRoles(value =  "teacher")
    @RequiresPermissions("roles:grades:update")
    @ApiOperation("老师修改学生的成绩")
    @PostMapping("/update")
    public R<String> updateGrades(@RequestBody GradesDto gradesDto){
        Grades grades = new Grades();

        //获取学生的studentId
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(Student::getName,gradesDto.getName());
        int studentId = studentService.getOne(studentLambdaQueryWrapper).getId();
        //获取课程的classId
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classLambdaQueryWrapper.eq(Class::getClassName,gradesDto.getClassName());
       int classId = classService.getOne(classLambdaQueryWrapper).getId();

       //获取courseId
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(Course::getCourseName,gradesDto.getCourseName());
        int courseId = courseService.getOne(courseLambdaQueryWrapper).getId();

        //获取examId
        LambdaQueryWrapper<Exam> examLambdaQueryWrapper = new LambdaQueryWrapper<>();
        examLambdaQueryWrapper.eq(Exam::getExamName,gradesDto.getExamName());
        int examId = examService.getOne(examLambdaQueryWrapper).getId();

        LambdaQueryWrapper<Grades> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        grades.setClassID(classId);
        grades.setCourseID(courseId);
        grades.setExamID(examId);
        grades.setStudentId(studentId);
        grades.setGrades(gradesDto.getGrades());
        gradesService.update(grades,lambdaQueryWrapper);
        return R.success("学生成绩修改成功");
    }



    //查看所有学生的成绩
@ApiOperation("查看所有学生的成绩")
@RequiresRoles(value =  "teacher")
@RequiresPermissions("roles:grades:page")
    @PostMapping("/page")
    public R<Page> dynamicQuery(Page<GradesDto> page, @RequestBody GradesDto gradesDto) {
       Page pageInfo = new Page(1,2);
        QueryWrapper<GradesDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(gradesDto.getName()), "s.name", gradesDto.getName());
        queryWrapper.like(StringUtils.hasText(gradesDto.getCourseName()), "co.courseName", gradesDto.getCourseName());
        queryWrapper.like(StringUtils.hasText(gradesDto.getClassName()), "cl.className", gradesDto.getClassName());
        queryWrapper.like(StringUtils.hasText(gradesDto.getExamName()), "e.examName", gradesDto.getExamName());

        queryWrapper.apply("t.classID = s.classID")
                .apply("cl.id = g.classID")
                .apply("co.id = g.courseID")
                .apply("e.id = g.examID");
        queryWrapper.groupBy("e.id");

        return R.success(gradesMapper.findBlog(page, queryWrapper));
    }

}
